<?php
/**
 * Bootstrap for Plurious Framework
 * 
 * NOTE: Do not instantiate your base object here. This file also acts as the
 * PHPUnit bootstrap. That means you should NOT combine the setup.php file
 * and this file. Otherwise you will break the testing environment. 
 */
/**
 * PHPUnit Setup must have the $_SERVER['DOCUMENT_ROOT'] set to operate properly
 * This is due to PHPUnit running in CLI mode
 * Set this for your own environment
 */
if (strpos($_SERVER['SCRIPT_FILENAME'], 'phpunit') !== false) {
    $_SERVER['SERVER_PROTOCOL'] = "http/1.0";
    $_SERVER['SERVER_NAME'] = "plurious";
    $_SERVER['DOCUMENT_ROOT'] = '/Library/WebStart/WWW/webroot';
    $_SERVER['PHP_SELF'] = '';
    if (!defined('PHPUNIT'))
        define('PHPUNIT', true);
}
/** 
 * Debug mode - PRODUCTION MODE = false 
 * Used by exceptions functions to provide or shield error msg output
 */
define('DEBUG', true);
/** 
 * Debug mode - PRODUCTION MODE = false 
 * Used by DB methods to provide or shield error msg output
 */
define('DATABASE_DEBUG', false);

/** Plurious Framework Version */
define('VERSION', '1.5');

// Check PHP Version
if (version_compare(phpversion(), '5.2', '<') == true) {
    exit('PHP 5.3+ Required');
}
//-------- OK Version is GOOD - Continue ----------//
/** Error Reporting and Exception Handling */
include_once('library/exceptions.php');
// Ensure PHP's timezone is set
if (!ini_get('date.timezone')) {
    date_default_timezone_set('UTC');
}
/**
 * Auto loader for Plurious Framework
 * 
 * Provides a simple, as designed loader based upon PHP namespace best practices
 * 
 * @param string $class The Class to load
 */
function plurious_autoload($namespaced_class = NULL) {
    if (stristr($namespaced_class, basename(SYSTEM_DIR))) {
        $filename = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespaced_class) . '.php';
    } else {
        $filename = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespaced_class) . '.php';
    }
    if (file_exists($filename)) {
        include $filename;
    }
}

// Register the Autoloader
spl_autoload_register('plurious_autoload', $throw = true, $prepend = true);

    /**
     * Returns either the full path to the class file or just the class name
     * 
     * @param string $namespace The fully qualified namespaced class
     * @param bool $fullpath Whether the full path is returned or not 
     */
function namespace2classname($namespaced_class){
    return strtolower(basename(str_replace('\\', DIRECTORY_SEPARATOR, $namespaced_class)));
}


/** Setup the namespaced elements */
include_once(dirname(__FILE__) . '/setup.php');
?>